#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <stdlib.h>

#define FRAME_RATE 25

extern void asmRoberts(const char* src, char* dst, int ancho, int alto,int);
extern void asmSobel(const char* src, char* dst, int ancho, int alto, int xorder, int yorder,int);
extern void asmPrewitt(const char* src, char* dst, int ancho, int alto,int);
//extern void asmWTF(const char* src, char* dst, int ancho, int alto);
//extern void asmMono(const char* src, char* dst, int ancho, int alto);

int main( int argc, char** argv ){

   IplImage * src = 0;
   IplImage * dst = 0;
   IplImage * dst_ini = 0;
   IplImage * dstR = 0;
   IplImage * dstG = 0;
   IplImage * dstB = 0;
	IplImage* frame2 = NULL;
	IplImage* frame1 = NULL;
	
	CvCapture* capture = NULL;
	IplImage* frame = NULL;
cvNamedWindow("camara",1);
	int state = 0;
	// create capture
	capture = cvCaptureFromCAM(0);
	if(!capture){
		printf("%s\n","Error al conectar la camara");
		return -1;
	}

IplImage* dst2 = NULL;

	if((src = cvCreateImage(cvSize(640,480), IPL_DEPTH_8U, 1))==0) return -1;
	if((dst = cvCreateImage(cvSize(640,480), IPL_DEPTH_8U, 1))==0) return -1;
	if((dst2 = cvCreateImage(cvSize(640,480), IPL_DEPTH_16S, 1))==0) return -1;
	
			

long int x = 0;
long int dif = 0;
long int dif2 = 0;
cvMoveWindow("camara",100,100);

	while(1){
	x++;
			frame = cvQueryFrame(capture);
	
	if(x==10){
	
	frame1 = cvQueryFrame(capture);
	}
	if(x == 5) 
	{
		frame2 = cvQueryFrame(capture);
		int i;
		int valor;
		for(i = 0;i < (frame2->widthStep)*(frame2->height);i = i+100){
			x = x + (int)(frame2->imageData)[i];
		}
		dif2 = dif;
		dif = x;
	
		valor =(dif2-dif)/1000;
		if(valor < 0) valor = -valor;
	
		printf("dif: %i x: %li",valor,x);
		
		if(valor == 0) 			printf(" Abran desconectado la camara?");	
		else if(valor < 20) 	printf(" tranquilidad");	
		else if(valor < 70) 	printf(" viento?");	
		else  	printf(" nos roban!");	



		printf("\n");		
		x= 0;
	}
		if(state==0){
			
			cvCvtColor(frame,dst,CV_BGR2GRAY); // convierte RGB a Grayscale
			
			
		}else if(state==1){
			cvCvtColor(frame,src,CV_BGR2GRAY); // convierte RGB a Grayscale
			asmSobel(src->imageData, dst->imageData, src->width, src->height, 1, 0,src->widthStep); 
		}else if(state==2){
			cvCvtColor(frame,src,CV_BGR2GRAY); // convierte RGB a Grayscale
			asmSobel(src->imageData, dst->imageData, src->width, src->height, 0, 1,src->widthStep); // aplico la mascara de sobel en Y
		}else if(state==3){
			cvCvtColor(frame,src,CV_BGR2GRAY); // convierte RGB a Grayscale
			asmSobel(src->imageData, dst->imageData, src->width, src->height, 1, 1,src->widthStep); // aplico la mascara de sobel en X e Y
		}else if(state==4){
			cvCvtColor(frame,src,CV_BGR2GRAY); // convierte RGB a Grayscale
			asmRoberts(src->imageData, dst->imageData, src->width, src->height,src->widthStep); // 
		}else if(state==5){
			cvCvtColor(frame,src,CV_BGR2GRAY); // convierte RGB a Grayscale
			asmPrewitt(src->imageData, dst->imageData, src->width, src->height,src->widthStep); // aplico la mascara Prewitt
		}else if(state==6){		
		
		}else if(state==7){
			


			
		
		}else if(state==8){
		}else if(state==9){
		}
		cvShowImage("camara",dst);
		int key = cvWaitKey(FRAME_RATE);
		if((char)key==27){
			break;
		}else if( '0'<=(char)key && (char)key<='9' ){
			state = atoi((char*)&key);
	
		}else if((char)key=='p'){
			cvSaveImage("printScreen.bmp", dst);
		printf("%s\n","output written to printScreen.jpg");
		}
	}
	cvReleaseCapture(&capture);
	cvDestroyWindow("camara");
		
   return 0;
}


